<?php
/**
 * Created by PhpStorm.
 * User: zhang-hong
 * Date: 2017/3/27
 * Time: 17:38
 * 体制调查
 */
class TizhisurveyController extends Yaf_Controller_Abstract
{
    public function IndexAction()
    {
        $request = $this->getRequest();
        $data = $request->getPost("info","");
        $yiyuan_id = $request->getPost("yiyuan_id","");
        $res['error'] = 200;
        $res['msg'] = "";

        //医院id判断
        if($yiyuan_id === "")
        {
            Utility::apiJsonError(20028, '请输入医院ID');
        }
        elseif(!ctype_digit("$yiyuan_id"))
        {
            Utility::apiJsonError(20027, '医院ID必须是整型');
        }
        if(!empty($data)) {
            $n=0;
            $temp = json_decode($data,true);
            //print_r($temp);exit;
            if(is_array($temp)&&count($temp)>0)
            {
                unset($temp[0]['version']);
                unset($temp[0]['user_number']);
                unset($temp[0]['session_id']);
                unset($temp[0]['hulijilu_id']);
                unset($temp[0]['biaodan_name']);
                $datas = $ins = $temp[0];

            }else{
                Utility::apiJsonError(20069, '数据为空');
            }

            if(!$datas['zhuyuan_id']){
                Utility::apiJsonError(20063, '住院ID必须是整型');
            }
            $pingcount =$qixucount = $qicount = $shicount = $tecount = $tancount = $xuecount= $yincount =$yangcount= 0;
            foreach($datas as $key=>$val){
                $datas[$key] = json_decode($val,true);
                if($datas[$key]['biaodan_leibie'] == '平和质' ){
                    $pingcount += 1;
                    $datas['pinghetotal'] += $datas[$key]['xiangmu_selection_svae_value'];
                }
                //$datas[$key] = $datas[$key]['xiangmu_selection_value'];

                if($datas[$key]['biaodan_leibie'] == '气虚质' ){
                    $qixucount += 1;
                    $datas['qixutotal'] += $datas[$key]['xiangmu_selection_svae_value'];
                }
                //$datas[$key] = $datas[$key]['xiangmu_selection_value'];

                if($datas[$key]['biaodan_leibie'] == '气郁质' ){
                    $qicount += 1;
                    $datas['qiyutotal'] += $datas[$key]['xiangmu_selection_svae_value'];
                }
                //$datas[$key] = $datas[$key]['xiangmu_selection_value'];

                if($datas[$key]['biaodan_leibie'] == '湿热质' ){
                    //$shicount += 1;
                    //logResult(serialize($datas[$key]['xiangmu_selection_svae_value']));
                    if($datas[$key]['xiangmu_selection_svae_value'] == "请选择") {

                    }else{
                        $shicount += 1;
                    }
                    $datas['shiretotal'] += $datas[$key]['xiangmu_selection_svae_value'];
                }
                //echo $shicount;die();
                //$datas[$key] = $datas[$key]['xiangmu_selection_value'];

                if($datas[$key]['biaodan_leibie'] == '特禀质' ){
                    $tecount += 1;
                    $datas['tebingtotal'] += $datas[$key]['xiangmu_selection_svae_value'];
                }
                //$datas[$key] = $datas[$key]['xiangmu_selection_value'];

                if($datas[$key]['biaodan_leibie'] == '痰湿质' ){
                    $tancount += 1;
                    $datas['tanshitotal'] += $datas[$key]['xiangmu_selection_svae_value'];
                    //logResult($datas['tanshitotal']);
                }
                //$datas[$key] = $datas[$key]['xiangmu_selection_value'];

                if($datas[$key]['biaodan_leibie'] == '血瘀质' ){
                    $xuecount += 1;
                    $datas['xueyutotal'] += $datas[$key]['xiangmu_selection_svae_value'];
                }
                //$datas[$key] = $datas[$key]['xiangmu_selection_value'];

                if($datas[$key]['biaodan_leibie'] == '阳虚质' ){
                    $yangcount += 1;
                    $datas['yangxutotal'] += $datas[$key]['xiangmu_selection_svae_value'];
                }
                //$datas[$key] = $datas[$key]['xiangmu_selection_value'];

                if($datas[$key]['biaodan_leibie'] == '阴虚质' ){
                    $yincount += 1;
                    $datas['yinxutotal'] += $datas[$key]['xiangmu_selection_svae_value'];
                }
                //$datas[$key] = $datas[$key]['xiangmu_selection_value'];
                $detail[$key] = $datas[$key]['xiangmu_selection_value'];
                $detail['zhuyuan_id'] = $datas['zhuyuan_id'];
            }

            $detail['jilu_hushi_name'] = $ins['jilu_hushi_name'];
            $detail['jilu_hushi_id'] =  $ins['jilu_hushi_id'];
            $detail['user_department'] =  $ins['user_department'];
            $detail['jilu_time'] = date('Y-m-d H:i:s',time());

            //转化分数=[(原始分-条目数)/(条目数*4)]*100
            if($datas['pinghetotal']){
                if(($datas['pinghetotal'] - $pingcount) == 0){
                    $pinghe = $detail['pinghefen'] = 0;
                }else {
                    $pinghe = $detail['pinghefen'] = round((($datas['pinghetotal'] - $pingcount) / ($pingcount * 4)) * 100);
                }
            }else{
                $pinghe = $detail['pinghefen'] =0;
            }
            if($datas['shiretotal']) {
                if(($datas['shiretotal'] - $shicount) == 0){
                    $shire = $detail['shirefen'] = 0;
                }else {
                    $shire = $detail['shirefen'] = round((($datas['shiretotal'] - $shicount) / ($shicount * 4)) * 100);
                }
            }else{
                $shire = $detail['shirefen'] = 0;
            }
            if($datas['tanshitotal']) {
                if(($datas['tanshitotal'] - $tancount) == 0){
                    $tanshi = $detail['tanshifen'] = 0;
                }else {
                    $tanshi = $detail['tanshifen'] = round((($datas['tanshitotal'] - $tancount) / ($tancount * 4)) * 100);
                }
            }else{
                $tanshi = $detail['tanshifen'] = 0;
            }
            if($datas['qixutotal']) {
                if(($datas['qixutotal'] - $qixucount) == 0){
                    $qixu = $detail['qixufen'] = 0;
                }else {
                    $qixu = $detail['qixufen'] = round((($datas['qixutotal'] - $qixucount) / ($qixucount * 4)) * 100);
                }
            }else{
                $qixu = $detail['qixufen'] = 0;
            }
            if($datas['xueyutotal']) {
                if(($datas['xueyutotal'] - $xuecount) == 0){
                    $xueyu = $detail['xueyufen'] = 0;
                }else {
                    $xueyu = $detail['xueyufen'] = round((($datas['xueyutotal'] - $xuecount) / ($xuecount * 4)) * 100);
                }
            }else{
                $xueyu = $detail['xueyufen'] = 0;
            }
            if($datas['tebingtotal']) {
                if(($datas['tebingtotal'] - $tecount) == 0){
                    $tebing = $detail['tebingfen'] = 0;
                }else {
                    $tebing = $detail['tebingfen'] = round((($datas['tebingtotal'] - $tecount) / ($tecount * 4)) * 100);
                }
            }else{
                $tebing = $detail['tebingfen'] =0;
            }
            if($datas['qiyutotal']) {
                if(($datas['qiyutotal'] - $qicount) == 0){
                    $qiyu = $detail['qiyufen'] = 0;
                }else {
                    $qiyu = $detail['qiyufen'] = round((($datas['qiyutotal'] - $qicount) / ($qicount * 4)) * 100);
                }
            }else{
                $qiyu = $detail['qiyufen'] =0;
            }
            if($datas['yinxutotal']) {
                if(($datas['yinxutotal'] - $yincount) == 0){
                    $yinxu = $detail['yinxufen'] = 0;
                }else {
                    $yinxu = $detail['yinxufen'] = round((($datas['yinxutotal'] - $yincount) / ($yincount * 4)) * 100);
                }
            }else{
                $yinxu = $detail['yinxufen'] = 0;
            }
            if($datas['yangxutotal']) {
                if(($datas['yangxutotal'] - $yangcount) == 0) {
                    $yangxu = $detail['yangxufen'] = 0;
                }else{
                    $yangxu = $detail['yangxufen'] = round((($datas['yangxutotal'] - $yangcount) / ($yangcount * 4)) * 100);
                }
            }else{
                $yangxu = $detail['yangxufen'] = 0;
            }
            //判定结果

            if($pinghe >= 60 && $shire < 30 && $tanshi <30 && $qixu <30 && $xueyu<30 && $tebing<30 &&$tebing<30&& $yinxu <30 && $yangxu<30){
                $detail['pinghe'] = '是';
            }elseif($pinghe >= 60 && $shire < 40 && $tanshi <40 && $qixu <40 && $xueyu<40 && $tebing<40 &&$tebing<40&& $yinxu <40 && $yangxu<40){
                $detail['pinghe'] = '基本是';
            }else{
                $detail['pinghe'] ='否';
            }

            if($yinxu >= 40){
                $detail['yinxu'] = '是';
            }elseif( $yinxu >= 30 && $yinxu < 40 ){
                $detail['yinxu'] = '倾向是';
            }else{
                $detail['yinxu'] = '否';
            }
            if($yangxu >= 40){
                $detail['yangxu'] = '是';
            }elseif( $yangxu >= 30 && $yangxu < 40 ){
                $detail['yangxu'] = '倾向是';
            }else{
                $detail['yangxu'] = '否';
            }
            if($qiyu >= 40){
                $detail['qiyu'] = '是';
            }elseif( $qiyu >= 30 && $qiyu < 40 ){
                $detail['qiyu'] = '倾向是';
            }else{
                $detail['qiyu'] = '否';
            }
            if($tebing >= 40){
                $detail['tebing'] = '是';
            }elseif( $tebing >= 30 && $tebing < 40 ){
                $detail['tebing'] = '倾向是';
            }else{
                $detail['tebing'] = '否';
            }
            if($xueyu >= 40){
                $detail['xueyu'] = '是';
            }elseif( $xueyu >= 30 && $xueyu < 40 ){
                $detail['xueyu'] = '倾向是';
            }else{
                $detail['xueyu'] = '否';
            }
            if($qixu >= 40){
                $detail['qixu'] = '是';
            }elseif( $qixu >= 30 && $qixu < 40 ){
                $detail['qixu'] = '倾向是';
            }else{
                $detail['qixu'] = '否';
            }
            if($tanshi >= 40){
                $detail['tanshi'] = '是';
            }elseif( $tanshi >= 30 && $tanshi < 40 ){
                $detail['tanshi'] = '倾向是';
            }else{
                $detail['tanshi'] = '否';
            }
            if($shire >= 40){
                $detail['shire'] = '是';
            }elseif( $shire >= 30 && $shire < 40 ){
                $detail['shire'] = '倾向是';
            }else{
                $detail['shire'] = '否';
            }
            $detail["yiyuan_id"] = $yiyuan_id;

            //print_r($detail);die();
            $insert = DB::insert('zhuyuan_tizhi_pinggudan_detail')
                ->columns(array_keys($detail))
                ->values(array_values($detail));
            list($insert_id, $affected_rows) = $insert->execute();
            if($insert_id){
                $res['state'] = 1;
                $res['id'] = $insert_id;
            }
            else{
                $res['state'] = 0;
            }
        }else{
            $res['state'] = 0;
        }
        Utility::apiJsonResultExtends($res);
    }
}